#include "oled.h"
#include "oledfont.h"
#include "i2c.h"
//#include "adc.h"
#include <stdio.h>

void WriteCmd(unsigned char I2C_Command)  //写命令
{
	uint8_t buf[2];
	buf[0] = 0x00;    
	buf[1] = I2C_Command;
  HAL_I2C_Master_Transmit (&hi2c1 ,OLED_ADDRESS, buf ,2,1000);	 //移植的标准库的I2C_WriteByte();  //主机发送写命令
	 
}


void WriteDat(unsigned char I2C_Data)  //写数据
{
	uint8_t buf[2];
	buf[0] = 0x40;    
	buf[1] = I2C_Data;
  HAL_I2C_Master_Transmit (&hi2c1 ,OLED_ADDRESS, buf ,2,1000);	 //移植的标准库的I2C_WriteByte();  //主机发送写数据

}


void OLED_Init(void)
{
  HAL_Delay(1);   //这里的延时很重要，使用HAL库替换，延时至少要100us,这里已给1ms.
	
	WriteCmd(0xAE);
	WriteCmd(0x20);
	WriteCmd(0x10);
	WriteCmd(0xb0);  //起始页地址
	WriteCmd(0xc8);
	WriteCmd(0x00);
	WriteCmd(0x10);
	WriteCmd(0x40);
	WriteCmd(0x81);
	WriteCmd(0xff);  //亮度调节 0x00~0xff
	WriteCmd(0xa1);
	WriteCmd(0xa6);
	WriteCmd(0xa8);
	WriteCmd(0x3F);
	WriteCmd(0xa4);
	WriteCmd(0xd3);
	WriteCmd(0x00);
	WriteCmd(0xd5);
	WriteCmd(0xf0);
	WriteCmd(0xd9);
	WriteCmd(0x22);
	WriteCmd(0xda);
	WriteCmd(0x12);
	WriteCmd(0xdb);
	WriteCmd(0x20);
	WriteCmd(0x8d);
	WriteCmd(0x14);
	WriteCmd(0xaf);
	OLED_CLS(); //清屏       //这行代码很重要,只要写下了,复位才会将原先的数据清除掉.
                         //是清屏哈,无现象的.(非全屏填充,全屏填充才有效果)
}


void OLED_SetPos(unsigned char x , unsigned char y)  //设置起始点坐标
{
	WriteCmd(0xb0+y);
	WriteCmd(((x&0xf0)>>4)|0x10);
	WriteCmd((x&0x0f)|0x01);
}


void OLED_Fill(unsigned char fill_Data) //全屏填充
{
	unsigned char m,n;
	for(m=0;m<8;m++)
	{
		WriteCmd(0xb0+m);
		WriteCmd(0x00);
		WriteCmd(0x10);
		for(n=0;n<128;n++)
		{
			WriteDat(fill_Data);
		}
	}
}


void OLED_CLS(void) //清屏
{
	OLED_Fill(0x00);  //清屏:就是将屏幕填充为0嘛.
}
	



void OLED_ON(void)   //将OLED从休眠中唤醒
{
	WriteCmd(0X8D);  //设置电荷泵
	WriteCmd(0X14);  //开启电荷泵
	WriteCmd(0XAF);  //OLED唤醒	
}


void OLED_OFF(void)   //将OLED从唤醒中休眠
{
	WriteCmd(0X8D);  //设置电荷泵
	WriteCmd(0x10);  //关闭电荷泵
	WriteCmd(0xAE);  //OLED唤醒
	
}

uint32_t OLED_Pow(unsigned char x,unsigned char y)
{
	uint32_t Result = 1;	//结果默认为1
	while (y --)			//累乘Y次
	{
		Result *= x;		//每次把X累乘到结果上
	}
	return Result;
}





//Parameters    :   x,y--起始点坐标(x:0~127,，y:0~7); ch[]--要显示的字符串; Textsize--字符大小(1:6*8 ;2:8*16)
// Description  :   显示oledfont.h中的ASCII参符,有6*8和8*16可选择
void OLED_ShowStr(unsigned char x,unsigned char y, unsigned char ch[], unsigned char TextSize)
{
  unsigned char c = 0, i = 0, j = 0;
	switch(TextSize)   //TextSize：只要1/2两个填法.
	{
		case 1:
		{
			while(ch[j] !='\0')
			{
				c = ch[j] - 32;
				if(x > 126)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<6;i++){
					WriteDat(F6x8[c][i]); }
				x += 6;
				j++;
			}
		}break;	
		case 2:
		{
			while(ch[j] !='\0')
			{
				c = ch[j] - 32;
				if(x > 120)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<8;i++){
					WriteDat(F8X16[c*16+i]); } 
				
				OLED_SetPos(x,y+1);
				for(i=0;i<8;i++){
				  WriteDat(F8X16[c*16+i+8]); }
				x += 8;
				j++;
			}
		}break;
	}
}



// Prototype    : void OLED ShowcN(unsigned char x,unsigned char y, unsigned char N)
// Calls
// Parameters   :x,y-- 起始点坐标(x:0~127，y:0~7);N:汉字在oledfont.h中的F16x16[]的下标
//Description   :显示oledfont.h中的汉字，16*16点阵
//这个屏幕最多可以显示4行汉字 ，每一行可以显示8个汉字 ，这个屏幕最多可以显示32个汉字

void OLED_showCN(unsigned char x,unsigned char y,unsigned char N)
{
  unsigned char wm=0;
  unsigned int adder=32*N;
   OLED_SetPos(x ,y);
    for(wm = 0;wm < 16;wm++)
	{
		WriteDat(F16x16[adder]); 
    adder += 1;
	}
   OLED_SetPos(x,y + 1);
    for(wm = 0;wm < 16;wm++)
	{
		WriteDat(F16x16[adder]); 
    adder += 1;
	}
}



//Prototype    : void OLED DrawBMP(unsigmed char x0,unsigned char y0,unsigned char x1,unsiged char yl,unsigmed char Bme[l);
//Parameters   :x0,y0-- 起始点坐标(x0:0~127，y0:0~7);x1,y1 -- 起点对角线(结東点)的坐标(x1:1~128,y1:1~8)
//Description  :显示BMP位图
void OLED_DrawBMP (unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
 unsigned int j=0;
 unsigned char x,y;	
	if(y1%8==0)
		y= y1/8;
	else
		y=y1/8 + 1;
	for (y=y0;y<y1;y++)
	{
	 OLED_SetPos(x0,y);
		for(x=x0;x<x1;x++)
		{
	   WriteDat(BMP[j++]);	
		}
	}
}


void OLED_ShowValue(unsigned char x, unsigned char y, int16_t value, unsigned char TextSize)  //实现更新传感器所读到的数据.
{
    unsigned char buf[10]; // 假设最大显示宽度不超过 10 个字符

    // 将整数转换为字符串
    sprintf((char*)buf, "%-05d", value);

    // 调用 OLED 显示字符串的函数
    OLED_ShowStr(x, y, buf, TextSize);
}




void OLED_Test(void)       
{
	 OLED_CLS(); //清屏
   OLED_Fill(0xFF); //全屏填充
		HAL_Delay(2000);
   OLED_Fill(0x00); //全屏熄灭
		HAL_Delay(2000);

  for(int i = 0; i<7 ; i++) //显示几个汉字
		{
       OLED_showCN(i*16,0,i);    
		}	
		HAL_Delay(2000);
		OLED_ShowStr(0,3,(unsigned char*)"OLED_Test", 1);
		OLED_ShowStr(0,4,(unsigned char*)"2023 05 28",2);
		HAL_Delay(2000);
		OLED_CLS(); //清屏
	  OLED_OFF();   //将OLED从唤醒中休眠
		HAL_Delay(2000);
    OLED_ON();   //将OLED从休眠中唤醒
    OLED_DrawBMP(0,0,128,8,(unsigned char*) BMP1);
		HAL_Delay(2000);
		
}

	 
extern uint32_t ADC_Valua;

